<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <common-item1
          :query="query"
          :streetList="streetList"
          :communityList="communityList"
          :villageList="villageList"
          @visibleChangeStreet="visibleChangeStreet"
          @changeStreet="changeStreet"
          @visibleChangeCommunity="visibleChangeCommunity"
          @changeCommunity="changeCommunity"
          @visibleChangeVillage="visibleChangeVillage"
          @changeVillage="changeVillage"
          :showInput="showInput"
          @search="handleSearch"
        />

        <el-select
          @change="selectGarbageType"
          v-model="query.garbageType"
          placeholder="品类"
          class="handle-select mr10"
          clearable
        >
          <el-option
            v-for="(item, index) in garbageType"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>

        <div class="block" style="float: left; margin-right: 10px;">
          <el-date-picker
            v-model="value2"
            type="daterange"
            align="left"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>

        <el-button type="primary" @click="handleSearch">查询</el-button>

        <el-button type="primary" @click="handleExport">导出</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          prop="index"
          label="序号"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="subDistrict"
          align="center"
          label="街道"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="communityName"
          label="社区"
        ></el-table-column>

        <el-table-column
          prop="villageName"
          align="center"
          label="所属小区"
        ></el-table-column>

        <el-table-column
          prop="dealNum"
          align="center"
          label="订单号"
        ></el-table-column>

        <el-table-column label="住室信息" width="100" align="center">
          <template slot-scope="scope">{{
            scope.row.buildingNo !== null
              ? scope.row.buildingNo +
                "_" +
                scope.row.unitNo +
                "_" +
                scope.row.houseNumber
              : null
          }}</template>
        </el-table-column>

        <el-table-column
          prop="garbageName"
          align="center"
          label="品类"
        ></el-table-column>

        <el-table-column
          prop="weight"
          align="center"
          label="重量(kg)"
        ></el-table-column>
        <el-table-column
          prop="weightTime"
          align="center"
          label="回收时间"
        ></el-table-column>

        <el-table-column
          prop="equipNo"
          align="center"
          label="设备编号"
        ></el-table-column>

        <el-table-column label="缩略图" align="center">
          <template slot-scope="scope">
            <el-image
              class="table-td-thumb"
              :src="scope.row.garbagePic"
              :preview-src-list="[scope.row.garbagePic]"
            ></el-image>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { commonItem1 } from "../../common/commonItem1/common-item1.js";
import NProgress from "nprogress";
export default {
  name: "collectmanage",
  data() {
    return {
      query: {
        streetId: "",
        communityId: "",
        villageId: "",

        streetName: "",
        communityName: "",
        villageName: "",

        buildingNum: "",
        unitNum: "",
        houseNum: "",

        pageIndex: 1,
        pageSize: 20,
        garbageType: "",
      },
      showInput: true,
      streetList: [],
      communityList: [],
      villageList: [],
      garbageType: [
        {
          name: "易腐垃圾",
          id: "1",
        },
        {
          name: "可回收物",
          id: "2",
        },
        {
          name: "其他垃圾",
          id: "3",
        },
        {
          name: "有害垃圾",
          id: "4",
        },
      ],
      type: "",
      value2: "",
      tableData: [],
      villageId: "",
      multipleSelection: [],
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1,
    };
  },
  created() {
    this.getData();
  },
  components: {
    "common-item1": () => import("../../common/commonItem1/common-item1.vue"),
  },
  methods: {
    ...commonItem1,

    selectGarbageType(type) {
      console.log(type);
      this.type = type;
    },
    getData() {
      NProgress.start();
      this.$axios
        .get("/village/garbagecollection/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            villageId: this.query.villageId,
            garbageType: this.type,
            createDate1: this.value2 ? this.value2[0].getTime().toString() : "",
            createDate2: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
            buildingNo: this.query.buildingNum,
            unitNo: this.query.unitNum,
            houseNumber: this.query.houseNum,
            villageName:
              this.showInput == false ? "" : this.query.villageName.trim(),
          },
        })
        .then(({ data: res }) => {
          console.log("数据", res);
          if (res.code != 0) {
            NProgress.done();
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
          this.pageTotal = res.data.total || 0;
          this.tableData.map((item, index) => {
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
            item.weightTime = this.dateFormat(item.weightTime);
            switch (item.garbageType) {
              case 1:
                item.garbageName = "易腐垃圾";
                break;
              case 2:
                switch (item.recycleType) {
                  case 21:
                    item.garbageName = "可回收物(金属)";
                    break;
                  case 22:
                    item.garbageName = "可回收物(塑料)";
                    break;
                  case 23:
                    item.garbageName = "可回收物(玻璃)";
                    break;
                  case 24:
                    item.garbageName = "可回收物(纺织)";
                    break;
                  case 25:
                    item.garbageName = "可回收物(纸类)";
                    break;
                  case 26:
                    item.garbageName = "可回收物(其他)";
                    break;
                  case 27:
                    item.garbageName = "可回收物(电子)";
                    break;
                }

                break;
              case 3:
                item.garbageName = "其他垃圾";
                break;
              case 4:
                item.garbageName = "有害垃圾";
                break;
            }
          });

          NProgress.done();
        })
        .catch(() => {});
    },
    handleExport() {
      if (
        !Boolean(this.query.streetId) &&
        this.query.villageName.trim() == ""
      ) {
        return this.$message.warning({
          message: "请选择查询的街道,社区,小区",
          duration: 1500,
        });
      }
      NProgress.start();
      this.$axios
        .get("/village/garbagecollection/export", {
          params: {
            villageId: this.query.villageId,
            garbageType: this.type,
            createDate1: this.value2 ? this.value2[0].getTime().toString() : "",
            createDate2: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
            buildingNo: this.query.buildingNum,
            unitNo: this.query.unitNum,
            houseNumber: this.query.houseNum,
            villageName:
              this.showInput == false ? "" : this.query.villageName.trim(),
          },
          responseType: "blob",
        })
        .then((res) => {
          console.log(res);
          if (res.data.type != "application/vnd.ms-excel") {
            NProgress.done();
            return this.$message.warning({
              message: res.msg,
              duration: 1500,
            });
          }

          // 导出excel表格
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          let num = "";
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10);
          }
          link.setAttribute("download", "用户_" + num + ".xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);

          NProgress.done();
        })
        .catch((err) => {});
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      // if (
      //   this.query.villageName.trim() == "" &&
      //   this.query.villageId.trim() == ""
      // ) {
      //   return this.$message.warning({
      //     message: "请先选择小区",
      //     duration: 1500,
      //   });
      // }
      this.getData();
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 分页导航
    handlePageChange(val) {
      console.log(val);
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getData();
    },
  },
  watch: {
    "$route.path": () => {},
  },
};
</script>

<style scoped lang="scss">
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 120px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
<style lang="scss">
.el-image-viewer__btn.el-image-viewer__close {
  // top: 28%;
  // right: 35%;
  color: #fff;
}
</style>
